package fms.dao;

import fms.model.Naip;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;


/**
 * Author1:ZhangJianPing  Time:11-9-14,下午5:10
 * Author2:hexun          Date:14-6-20
 * Author3:Yerb          Date:14-6-23
 */
@Service("naipService")
public class NaipServiceImpl implements NaipService {
    private Logger windCalcLogger = Logger.getLogger("windCalc");

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private NaipDao naipDao;

    // 新增
    // 自动提交事务
    @Transactional
    public Naip save(Naip naip) {
        return naipDao.save(naip);
    }


    @Transactional
    public Iterable<Naip> save(Iterable<Naip> naip) {
        return naipDao.save(naip);
    }

    // 新增 + 更新
    @Transactional
    public Naip update(Naip naip) {
        return naipDao.saveAndFlush(naip);
    }

    @Override
    public long count() {
        return naipDao.count();
    }

    @Transactional
    @Override
    public void delete(Naip naip) {
        naipDao.delete(naip);
    }

    @Transactional
    @Override
    public void deleteById(int id) {
        naipDao.deleteById(id);
    }

    @Override
    public Naip findByCptName(String cptname) {
        return naipDao.findByCptName(cptname);
    }

    @Transactional
    @Override
    public void deleteAll() {
        naipDao.deleteAll();
    }

    @Override
    public Iterable<Naip> findAll() {
        return naipDao.findAll();
    }

    @Override
    public Naip findById(int id) {
        return naipDao.findById(id);
    }

    @Override
    public List<Naip> findByCptnameLike(String cptnamelike) {
        return naipDao.findByCptnameLike(cptnamelike);
    }

    //取得航路点的经度、纬度
    @Override
    public List findCptPosByName(String cptname) {
        StringBuffer str = new StringBuffer();
        str.append("    select u.lon, u.lat ");
        str.append("    from naip u ");
        str.append("    where u.CPT_NAME = :cptName ");

        Query query = em.createNativeQuery(str.toString());
        query.setParameter("cptName", cptname);

//        windCalcLogger.debug("查询航路点(" + cptname + ")经纬度数据");
//        windCalcLogger.debug("航路点经纬度查询sql:" + str);

        return query.getResultList();
    }

}
